package com.smartcruise.task.completedTask;

import com.smartcruise.base.RefreshContract;
import com.smartcruise.data.PageHistory;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.task.TaskAdapter;
import com.smartcruise.util.Key;
import com.smartcruise.util.TempleUser;
import com.smartcruise.util.network.callBack.SimpleCallBack;
import com.smartcruise.util.network.netUtils.NetCollection;
import com.smartcruise.util.network.netUtils.Result;

import io.realm.ObjectChangeSet;
import io.realm.Realm;
import io.realm.RealmModel;
import io.realm.RealmObjectChangeListener;
import io.realm.RealmResults;

/**
 * Created by FengChaoQun
 * on 2018/1/22 0022
 */

public class CompletedTaskPresenterImp implements RefreshContract.Presenter {

    private RefreshContract.View mView;
    private PageHistory pageHistory;
    private RefreshContract.RefreshInterface refreshInterface;

    public CompletedTaskPresenterImp(RefreshContract.View mView) {
        this.mView = mView;
        refreshInterface = mView.getRefresh();
    }

    @Override
    public void start() {

        pageHistory = mView.getRealm().where(PageHistory.class)
                .equalTo(Key.userId, TempleUser.getInstance().getAccount())
                .equalTo(Key.type, PageHistory.TYPE_COMPLETED_TASK)
                .findFirst();

        if (pageHistory == null) {
            mView.getRealm().executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    PageHistory newPageHistory = new PageHistory();
                    newPageHistory.setType(PageHistory.TYPE_COMPLETED_TASK);
                    newPageHistory.setUserId(TempleUser.getInstance().getAccount());
                    pageHistory = realm.copyToRealmOrUpdate(newPageHistory);
                }
            });
        }

        pageHistory.addChangeListener(new RealmObjectChangeListener<RealmModel>() {
            @Override
            public void onChange(RealmModel realmModel, ObjectChangeSet changeSet) {
                refreshInterface.setNoMoreData(!pageHistory.hasNextData());
            }
        });

        RealmResults<CruiseData> all = mView.getRealm().where(CruiseData.class)
                .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                .equalTo(Key.auditStatus, CruiseContract.AUDITED)
                .equalTo(Key.auditResult, CruiseContract.AUDIT_SUCCESS)
                .findAll();
        TaskAdapter taskAdapter = new TaskAdapter(mView.getActivity(), all);
        mView.setData(taskAdapter);

        if (pageHistory.needRefresh()) {
            refreshInterface.autoRefresh();
        }
    }

    /**
     * description:下拉刷新
     */
    @Override
    public void refresh() {
        NetCollection.getCompletedTask(1, mView, new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {

            }
        });
    }

    @Override
    public void loadMore() {
            NetCollection.getCompletedTask(pageHistory.getCurrentPage() + 1, mView, new SimpleCallBack(mView) {
                @Override
                public void onSuccess(Result result) {

                }
            });
    }

    /**
     * description:界面销毁的时候调用
     */
    @Override
    public void destroy() {
        if (pageHistory != null) {
            pageHistory.removeAllChangeListeners();
        }
    }
}
